﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.Text;
using MySQL.Db;

namespace Gollie.Invoice
{
	public class InvoiceCalculatorCollection : Dictionary<uint, InvoiceCalculator>
	{
		public InvoiceCalculatorCollection(int month, int year)
		{
			ResultSet resultSetDeposits = Database.Queries.GetDepositsForInvoice(month, year);
			if (resultSetDeposits.Count == 0)
				return;

			uint prevVendorId = resultSetDeposits.GetUInt32(0, "vendorid");

			InvoiceCalculator invoiceCalculator = new InvoiceCalculator(prevVendorId);
			
			for (int i = 0; i < resultSetDeposits.Count; i++)
			{
				uint vendorId = resultSetDeposits.GetUInt32(i, "vendorid");
				if (vendorId != prevVendorId)
				{
					invoiceCalculator.FinalizeCalculation();

					this[prevVendorId] = invoiceCalculator;

					prevVendorId = vendorId;

					invoiceCalculator = new InvoiceCalculator(vendorId);
				}

				decimal deposit = resultSetDeposits.GetDecimal(i, "deposit");

				DateTime paydate = resultSetDeposits.GetDateTime(i, "paydate");

				invoiceCalculator.AddDeposit(deposit, paydate);
			}

			invoiceCalculator.FinalizeCalculation();

			this[prevVendorId] = invoiceCalculator;
		}
	}
}
